<?php

/*
 * This file is part of the RADHealth Code Set Manager .
 * (c) Robin S. Carriere <rscarriere@stemos.com>
 * 
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/**
 * rhWidgetFormGenCodesSelect represents a select HTML tag for a model.
 *
 * @package    symfony
 * @subpackage widget
 * @author     Robin S. Carriere <rscarriere@stemos.com>
 * @version    SVN: $Id: rhWidgetFormGenCodesSelect.class.php 7746 2008-03-05 12:10:27Z fabien $
 */
class rhWidgetGenCodeSelect extends sfWidgetFormSelect
{
  /**
   * @see sfWidget
   */
  public function __construct($options = array(), $attributes = array())
  {
    $options['choices'] = new sfCallable(array($this, 'getChoices'));

    parent::__construct($options, $attributes);
  }

  /**
   * Constructor.
   *
   * Available options:
   *
   *  * model:      The model class (required)
   *  * add_empty:  Whether to add a first empty value or not (false by default)
   *                If the option is not a Boolean, the value will be used as the text value
   *  * method:     The method to use to display object values (__toString by default)
   *  * order_by:   An array composed of two fields:
   *                  * The column to order by the results (must be in the PhpName format)
   *                  * asc or desc
   *  * alias:      The alias for the main component involved in the query
   *  * query:      A query to use when retrieving objects
   *  * connection: The Doctrine connection to use (null by default)
   *  * multiple:   true if the select tag must allow multiple selections
   *
   * @see sfWidgetFormSelect
   */
  protected function configure($options = array(), $attributes = array())
  {
    $this->addRequiredOption('code_set_nm');
    $this->addOption('add_empty', false);
    $this->addOption('status_cd', array('A'));

    parent::configure($options, $attributes);
  }

  /**
   * Returns the choices associated to the model.
   *
   * @return array An array of choices from the CM general code value table
   */
  public function getChoices()
  {
    $status = "('".implode("','", $this->getOption('status_cd'))."')"; 
    $choices = CMGeneralCodeValue::getCodeValuesForSet($this->getOption('code_set_nm'), 
                                      array('for_dropdown' => 1, 'status_cd' => $status));
    $empty = array();
    if (false !== $this->getOption('add_empty'))
    {
      $empty[''] = true === $this->getOption('add_empty') ? '' : $this->getOption('add_empty');
    }
    
    $choices = array_merge($empty, $choices);
    
    if (false == $this->getOption('add_empty') && false !== $this->getOption('default'))
    {
      $default = CMGeneralCodeValue::getCodeValue(
                    $this->getOption('code_set_nm'),
                    $this->getOption('default'),
                    array('for_dropdown' => 1, 
                    'status_cd' => $status));
      if(!empty($default))
        $choices = array_merge($default, $choices);
    }
    
    return $choices;
  }

  public function __clone()
  {
    $this->setOption('choices', new sfCallable(array($this, 'getChoices')));
  }
}
?>
